"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse工具输出检查报告功能
Create At   : 2024/10/21
Owner       : lihongji
Description :
    1.A1~A10的检测项输出到report.txt文件中
    2.A1~A10的检测项打印到屏幕
    3.B1~B10的修复结果输出到repair.txt文件中
    4.B1~B10的修复结果打印至屏幕
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from yat.test import Node
from yat.test import macro

class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH
        self.report_txt = 'report.txt'
        self.repair_txt = 'repair.txt'
        self.report_path = os.path.join(macro.DB_BACKUP_PATH, self.report_txt)
        self.repair_path = os.path.join(macro.DB_BACKUP_PATH, self.repair_txt)


    def test_paralle(self):
        step = '----step1:A1~A10的检测项输出到report.txt文件中 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A --detail -o {self.report_txt}')
        self.log.info(gs_checkse_cmd)
        report_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Operation system check/set is completed.",
                      report_res, '执行失败' + step)

        step = '----step2:A1~A10的检测项打印到屏幕 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path}; gs_checkse -i A --detail')
        self.log.info(gs_checkse_cmd)
        report_res = self.common.get_sh_result(self.primaryNode,gs_checkse_cmd)
        self.assertIn("A1. [ Checking Connection configuration ]",
                      report_res, '执行失败' + step)
        self.assertIn("A10.[ Other configurations ]",
                      report_res, '执行失败' + step)

        step = '----step3:B1~B10的修复结果输出到repair.txt文件中 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B --detail -o {self.repair_txt}')
        self.log.info(gs_checkse_cmd)
        report_res = self.common.get_sh_result(self.primaryNode,
                                               gs_checkse_cmd)
        self.assertIn("Operation system check/set is completed.",
                      report_res, '执行失败' + step)

        step = '----step4:B1~B10的修复结果打印至屏幕 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};gs_checkse -i B --detail')
        self.log.info(gs_checkse_cmd)
        report_res = self.common.get_sh_result(self.primaryNode,
                                               gs_checkse_cmd)
        self.assertIn("B1. [ Set Connection configuration ]",
                      report_res, '执行失败' + step)
        self.assertIn("B10.[ Set Other configurations ]",
                      report_res, '执行失败' + step)

    def tearDown(self):
        step = '----step5:清理生成的安全检测报告与修复报告 expect:成功----'
        self.log.info(step)
        del_file = f"rm -rf {self.report_path} {self.repair_path}"
        self.log.info(del_file)
        del_res = self.common.get_sh_result(self.primaryNode, del_file)
        self.assertEqual('', del_res, '执行失败：' + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')
